package com.medicalfee.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.medicalfee.dao.StuffInfo;
import com.medicalfee.dao.StuffInfoDAO;
import com.medicalfee.util.BaseAmountCount;

//重新计算年度报销额度
public class ResetAmountController implements Controller {
	private StuffInfoDAO stuffInfoDao;

	public void setStuffInfoDao(StuffInfoDAO stuffInfoDao) {
		this.stuffInfoDao = stuffInfoDao;
	}

	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		List result = stuffInfoDao.getStuffByHql("from StuffInfo");
		for (int i = 0; i < result.size(); i++) {
			StuffInfo stuff = (StuffInfo) result.get(i);
			// 重置门诊和住院报销金额
			stuff.setReimbursedByHospitalized(0);
			stuff.setReimbursedByOutpatient(0);
			// 重置当前年份
			SimpleDateFormat format = new SimpleDateFormat("yyyy");
			int currentYear = Integer.parseInt(format.format(new Date()));
			stuff.setCurrentYear(currentYear);
			// 计算报销额度
			// 空勤人员和离休人员为全额包销，不设最高额度，以-1表示
			String job = stuff.getJob();
			String level = stuff.getLevel();
			int workAttendYear = stuff.getYearAttendingJob();
			if (job.equals("离休") || stuff.isAircrew()) {
				stuff.setAlgorithmStandard(-1);
				stuff.setReimbursementPerYear(-1);
			} else if (job.equals("独生子女")) {
				stuff.setReimbursementPerYear(BaseAmountCount
						.baseAmountOfChild(workAttendYear, currentYear));
			} else if (level.equals("厅级")) {
				stuff.setReimbursementPerYear(BaseAmountCount
						.baseAmountOfStuff(workAttendYear, currentYear) * 1.1);
			} else {
				stuff.setReimbursementPerYear(BaseAmountCount
						.baseAmountOfStuff(workAttendYear, currentYear));
			}
			// 重置床位费标准
			if (!job.equals("离休") && !stuff.isAircrew()) {
				if (level.equals("厅级")) {
					stuff.setAlgorithmStandard(150);
				} else if (level.equals("处级")) {
					stuff.setAlgorithmStandard(100);
				} else {
					stuff.setAlgorithmStandard(70);
				}
			}
			stuffInfoDao.insertOrUpdateStuff(stuff);
		}
		JSONArray json = new JSONArray();
		JSONObject jobject = new JSONObject();
		jobject.put("success", true);
		json.add(jobject);
		response.getWriter().print(json.toString());
		return null;
	}

}
